﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lottery.MediaData.Core {
    public static class SCHelper {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <returns>汇总数据，Key:差数， Value:场数</returns>
        public static Dictionary<int, int> ProcessAllData(List<SC> data) {
            var result = new Dictionary<int, int>();
            foreach (var item in data) {
                var d = item.D;
                var z = item.Z;
                var x = item.X;

                if (d != string.Empty) {
                    var k = Convert.ToInt32(d);
                    if (result.ContainsKey(k)) {
                        result[k] = ++result[k];
                    } else {
                        result.Add(k, 1);
                    }
                }

                if (z != string.Empty) {
                    var k = Convert.ToInt32(z);
                    if (result.ContainsKey(k)) {
                        result[k] = ++result[k];
                    } else {
                        result.Add(k, 1);
                    }
                }

                if (x != string.Empty && x != z) {
                    var k = Convert.ToInt32(x);
                    if (result.ContainsKey(k)) {
                        result[k] = ++result[k];
                    } else {
                        result.Add(k, 1);
                    }
                }
            }

            var o = from item in result
                    orderby item.Key descending
                    select item;
            result = new Dictionary<int, int>();
            foreach (var item in o) {
                result.Add(item.Key, item.Value);
            }

            return result;
        }

        public static FTCellData Gen(FTCellData input, List<SC> sc, List<FD> fd, string subIndex) {
            int index = input.RowCount;
            int counter = 1;
            int lastCounter = fd.Count;

            foreach (var item in fd) {
                var dataInput = new List<string>();

                var mediaCount = item.MediaCount.ToString();

                dataInput.Add((++index).ToString());

                foreach (var data in sc) {

                    StringBuilder sb = new StringBuilder();
                    if (data.D == mediaCount) {
                        sb.Append(data.GetDaShu310());
                    }

                    if (data.Z == mediaCount)
                        sb.Append(data.GetZhongShu310());

                    if (data.X == mediaCount)
                        sb.Append(data.GetXiaoShu310());

                    string cell = sb.ToString();
                    dataInput.Add(cell);
                }
                StringBuilder line = new StringBuilder();
                line.Append(item.Low);
                line.Append("-");
                line.Append(item.Up);

                dataInput.Add(line.ToString());

                // '正确'列的占位符
                dataInput.Add(string.Empty);

                // 限制名称。(仅仅在开始一行和最后一行加上限制名称)
                if (counter == 1 || counter == lastCounter)
                    dataInput.Add(string.Format("{1} 表2-{0}", subIndex, item.MediaCount));
                else
                    dataInput.Add(item.MediaCount.ToString());

                counter++;

                input.AddRow(dataInput);
            }

            return input;
        }

        public static FTCellData CombineLine(FTCellData input, List<FD> fs, CL c) {
            return MDHelper.CombineLine(input, fs, c);
        }
    }
}
